package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回服务器时间
 */
public class System_ServerTimeReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 14002; }

    public Float LocalTime; //客户端发送的本地时间(毫秒)
    public Long ServerTime; //服务器时间(毫秒)

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteFloat(LocalTime);
            dataStream.WriteLong(ServerTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static System_ServerTimeReturnProto GetProto(byte[] buffer) {
        System_ServerTimeReturnProto proto = new System_ServerTimeReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.LocalTime = dataStream.ReadFloat();
            proto.ServerTime = dataStream.ReadLong();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}